package edu.calpoly.android.lab2;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.ViewGroup.LayoutParams;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;


public class SimpleJokeList extends Activity implements OnKeyListener{

	/** Contains the list Jokes the Activity will present to the user **/
	protected ArrayList<Joke> m_arrJokeList;

	/**
	 * LinearLayout used for maintaining a list of Views that each display Jokes
	 **/
	protected LinearLayout m_vwJokeLayout;

	/**
	 * EditText used for entering text for a new Joke to be added to
	 * m_arrJokeList.
	 **/
	protected EditText m_vwJokeEditText;

	/**
	 * Button used for creating and adding a new Joke to m_arrJokeList using the
	 * text entered in m_vwJokeEditText.
	 **/
	protected Button m_vwJokeButton;
	
	/**
	 * Background Color values used for alternating between light and dark rows
	 * of Jokes.  
	 */
	protected int m_nDarkColor;
	protected int m_nLightColor;
	
	/**
	 * Boolean used for alternating between light and dark rows
	 */
	protected boolean m_bDark;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// TODO
		Resources resources = this.getResources();
		
		m_arrJokeList = new ArrayList<Joke>();
		m_bDark = false;
		
		initLayout();
		initAddJokeListeners();
		
		String[] m_arrjokes = resources.getStringArray(R.array.jokeList);
		
		m_nDarkColor = resources.getColor(R.color.dark);
		m_nLightColor = resources.getColor(R.color.light);
		
		for (String strJoke : m_arrjokes) {
			addJoke(strJoke);
		}
		
		
	}
	
	/**
	 * Method used to encapsulate the code that initializes and sets the Layout
	 * for this Activity. 
	 */
	protected void initLayout() {
		// TODO
		LinearLayout m_rootJokeLayout = new LinearLayout(this);
		m_rootJokeLayout.setOrientation(LinearLayout.VERTICAL);
		
		LinearLayout m_headerJokeLayout = new LinearLayout(this);
		m_headerJokeLayout.setOrientation(LinearLayout.HORIZONTAL);
		
		/* Initialize the Button */
		m_vwJokeButton = new Button(this);
		m_vwJokeButton.setText("Add Joke");
		
		/* Initialize the Text */
		m_vwJokeEditText = new EditText(this);
		LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
		m_vwJokeEditText.setLayoutParams(params);
		
		/* Add elements to the header LinearLayout */
		m_headerJokeLayout.addView(m_vwJokeButton);
		m_headerJokeLayout.addView(m_vwJokeEditText);
		
		/* Add horizontal LinearLayout to your root ViewGroup. */
		m_rootJokeLayout.addView(m_headerJokeLayout);
		
		
		m_vwJokeLayout = new LinearLayout(this);
		m_vwJokeLayout.setOrientation(LinearLayout.VERTICAL);
		
		ScrollView m_svJokeLayout = new ScrollView(this);
		m_svJokeLayout.addView(m_vwJokeLayout);
		
		m_rootJokeLayout.addView(m_svJokeLayout);
		
		setContentView(m_rootJokeLayout);		
	}
	
	/**
	 * Method used to encapsulate the code that initializes and sets the Event
	 * Listeners which will respond to requests to "Add" a new Joke to the 
	 * list. 
	 */
	protected void initAddJokeListeners() {
		// TODO
		
//		m_vwJokeEditText.setOnKeyListener(new OnKeyListener() {
//			
//			@Override
//			public boolean onKey(View v, int keyCode, KeyEvent event) {
//				// TODO Auto-generated method stub
//				if(KeyEvent.KEYCODE_ENTER == event.getKeyCode())
//				{
//					String m_sNewJoke = m_vwJokeEditText.getText().toString();
//					
//					if(!(m_sNewJoke.length() == 0 || m_sNewJoke.replaceAll("\n", "").length() == 0))
//					{
//						/* Limpa o texto do EditText */
//						m_vwJokeEditText.setText("");
//						
//						addJoke(m_sNewJoke);					
//						
//					}
//						
//					InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//					imm.hideSoftInputFromWindow(m_vwJokeEditText.getWindowToken(), 0);
//	
//				}
//				return false;
//			}
//		});
		
		m_vwJokeEditText.setOnKeyListener(this);

		m_vwJokeButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View view) {
				// TODO Auto-generated method stub
				String m_sNewJoke = m_vwJokeEditText.getText().toString();
				
				if(m_sNewJoke.length() == 0)
					return;
				
				/* Limpa o texto do EditText */
				m_vwJokeEditText.setText("");
				
				addJoke(m_sNewJoke);
				
				InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
				imm.hideSoftInputFromWindow(m_vwJokeEditText.getWindowToken(), 0);
				
			}
		});
		
	}

	/**
	 * Method used for encapsulating the logic necessary to properly initialize
	 * a new joke, add it to m_arrJokeList, and display it on screen.
	 * 
	 * @param strJoke
	 *            A string containing the text of the Joke to add.
	 */
	protected void addJoke(String strJoke) {
		// TODO
		Joke joke = new Joke(strJoke);
		m_arrJokeList.add(joke);
		
		TextView m_tvJokeLayout = new TextView(this);
		m_tvJokeLayout.setText(joke.getJoke());
		m_tvJokeLayout.setTextSize(16);
		
		if(m_bDark)
			m_tvJokeLayout.setBackgroundColor(m_nDarkColor);
		else
			m_tvJokeLayout.setBackgroundColor(m_nLightColor);
	
		m_bDark = !m_bDark;
		
		m_vwJokeLayout.addView(m_tvJokeLayout);
	}

	@Override
	public boolean onKey(View v, int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(KeyEvent.KEYCODE_ENTER == event.getKeyCode() || (event.getAction() == KeyEvent.ACTION_UP && KeyEvent.KEYCODE_DPAD_CENTER == event.getKeyCode()))
		{
			String m_sNewJoke = m_vwJokeEditText.getText().toString();

			if(!(m_sNewJoke.length() == 0 || m_sNewJoke.replaceAll("\n", "").length() == 0))
			{
				/* Limpa o texto do EditText */
				m_vwJokeEditText.setText("");

				addJoke(m_sNewJoke);					

			}

			InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(m_vwJokeEditText.getWindowToken(), 0);

		}
		return false;
	}
}